#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <iostream>

using namespace std;

class Solution 
{
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) 
    {
        vector<vector<string>> result;
        unordered_map<string, vector<string>> groups;
        for(int i = 0; i < strs.size(); i++)
        {
            string s = strs[i];
            sort(s.begin(),s.end());
            auto it = groups.find(s);
            if(it == groups.end())
            {
                vector<string> v;
                v.push_back(strs[i]);
                groups.emplace(s, v);
            }
            else
            {
                it->second.push_back(strs[i]);
            }
        }
        for(auto it = groups.begin(); it != groups.end(); it++)
        {
            result.push_back(it->second);
        }
        return result;
    }
};

// 辅助函数：打印结果
void printResult(const vector<vector<string>>& result) 
{
    cout << "[";
    for(int i = 0; i < result.size(); i++) 
    {
        cout << "[";
        for(int j = 0; j < result[i].size(); j++) 
        {
            cout << "\"" << result[i][j] << "\"";
            if(j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if(i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main() 
{
    Solution solution;
    
    // 测试案例1: ["eat","tea","tan","ate","nat","bat"]
    cout << "测试案例1:" << endl;
    vector<string> strs1 = {"eat", "tea", "tan", "ate", "nat", "bat"};
    cout << "输入: [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]" << endl;
    vector<vector<string>> result1 = solution.groupAnagrams(strs1);
    cout << "输出: ";
    printResult(result1);
    cout << "期望: [[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]]" << endl << endl;
    
    // 测试案例2: [""]
    cout << "测试案例2:" << endl;
    vector<string> strs2 = {""};
    cout << "输入: [\"\"]" << endl;
    vector<vector<string>> result2 = solution.groupAnagrams(strs2);
    cout << "输出: ";
    printResult(result2);
    cout << "期望: [[\"\"]]" << endl << endl;
    
    // 测试案例3: ["a"]
    cout << "测试案例3:" << endl;
    vector<string> strs3 = {"a"};
    cout << "输入: [\"a\"]" << endl;
    vector<vector<string>> result3 = solution.groupAnagrams(strs3);
    cout << "输出: ";
    printResult(result3);
    cout << "期望: [[\"a\"]]" << endl << endl;
    
    // 测试案例4: 复杂案例
    cout << "测试案例4 (复杂案例):" << endl;
    vector<string> strs4 = {"abc", "bca", "cab", "xyz", "zyx", "yxz", "def"};
    cout << "输入: [\"abc\",\"bca\",\"cab\",\"xyz\",\"zyx\",\"yxz\",\"def\"]" << endl;
    vector<vector<string>> result4 = solution.groupAnagrams(strs4);
    cout << "输出: ";
    printResult(result4);
    cout << "期望: [[\"abc\",\"bca\",\"cab\"],[\"xyz\",\"zyx\",\"yxz\"],[\"def\"]]" << endl << endl;
    
    // 测试案例5: 单字符重复
    cout << "测试案例5 (单字符重复):" << endl;
    vector<string> strs5 = {"aaa", "aa", "a", "aaa", "aa"};
    cout << "输入: [\"aaa\",\"aa\",\"a\",\"aaa\",\"aa\"]" << endl;
    vector<vector<string>> result5 = solution.groupAnagrams(strs5);
    cout << "输出: ";
    printResult(result5);
    cout << "期望: [[\"aaa\",\"aaa\"],[\"aa\",\"aa\"],[\"a\"]]" << endl << endl;
    
    // 测试案例6: 长字符串异位词
    cout << "测试案例6 (长字符串):" << endl;
    vector<string> strs6 = {"abcdef", "fedcba", "listen", "silent", "hello"};
    cout << "输入: [\"abcdef\",\"fedcba\",\"listen\",\"silent\",\"hello\"]" << endl;
    vector<vector<string>> result6 = solution.groupAnagrams(strs6);
    cout << "输出: ";
    printResult(result6);
    cout << "期望: [[\"abcdef\",\"fedcba\"],[\"listen\",\"silent\"],[\"hello\"]]" << endl << endl;
    
    return 0;
}